package com.shenfc.fxzj.action.admin;

import com.alibaba.fastjson.JSONObject;
import com.shenfc.fxzj.constants.CodeConstants;
import com.shenfc.fxzj.domain.BlogType;
import com.shenfc.fxzj.service.BlogService;
import com.shenfc.fxzj.service.BlogTypeService;
import com.shenfc.fxzj.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 管理员系统Controller层
 *
 * @author Administrator
 */
@Controller
@RequestMapping("/admin")
public class SystemAdminAction extends BaseAction {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private BlogTypeService blogTypeService;

    @Autowired
    private BlogService blogService;

    /**
     * 刷新系统缓存
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/refreshSystem")
    @ResponseBody
    public JSONObject refreshSystem(HttpServletResponse response, HttpServletRequest request) throws Exception {
        ServletContext application = RequestContextUtils.findWebApplicationContext(request).getServletContext();
        List<BlogType> blogTypeCountList = blogTypeService.queryBlogTypeList(); // 查询博客类别以及博客的数量
        application.setAttribute("blogTypeCountList", blogTypeCountList);

        redisUtil.del(CodeConstants.REDIS_SHIRO_CACHE + "admin@qq.com");

        JSONObject result = new JSONObject();
        result.put("success", true);
        return result;
    }
}
